/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html.xhtml.form;

import java.io.IOException;

import pikes.core.Printable;
import pikes.core.PrintableCharSequence;
import pikes.html.xhtml.CoreAttributes;
import pikes.html.xhtml.Events;
import pikes.html.xhtml.I18N;
import pikes.util.Assert;
import pikes.xml.XMLTag;

/**
 * <code>&lt;input/&gt;</code> tag, see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.4">INPUT
 * element</a> in HTML specification.
 * @author Peter Bona
 * @see <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.4">The INPUT element in HTML specification</a>
 */
public class Input extends FormControl implements CoreAttributes, I18N, Events {

	/**
	 * Type of control. see <a href="http://www.w3.org/TR/html401/interact/forms.html#adef-type-INPUT">type attribute</a>
	 * @author Peter Bona
	 */
	public interface InputType extends Printable {

		/**
		 * PCData input type
		 */
		InputType TEXT = new InputType() {

			@Override
			public String toString() {
				return "text";
			}

			public final void print(Appendable appendable) throws IOException {
				appendable.append("text");
			}

		};

		/**
		 * Password input type
		 */
		public InputType PASSWORD = new InputType() {

			public void print(Appendable appendable) throws IOException {
				appendable.append("password");
			}

		};

		/**
		 * Checkbox input type
		 */
		public InputType CHECKBOX = new InputType() {

			@Override
			public String toString() {
				return "checkbox";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("checkbox");
			}

		};

		/**
		 * Radio input type
		 */
		public InputType RADIO = new InputType() {

			@Override
			public String toString() {
				return "radio";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("radio");
			}

		};

		/**
		 * Submit input type
		 */
		public InputType SUBMIT = new InputType() {

			@Override
			public String toString() {
				return "submit";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("submit");
			}

		};

		/**
		 * Reset input type
		 */
		public InputType RESET = new InputType() {

			@Override
			public String toString() {
				return "reset";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("reset");
			}

		};

		/**
		 * File input type
		 */
		public InputType FILE = new InputType() {

			@Override
			public String toString() {
				return "file";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("file");
			}

		};

		/**
		 * Hidden input type
		 */
		public InputType HIDDEN = new InputType() {

			@Override
			public String toString() {
				return "hidden";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("hidden");
			}

		};

		/**
		 * Image input type
		 */
		public InputType IMAGE = new InputType() {

			@Override
			public String toString() {
				return "image";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("image");
			}

		};

		/**
		 * Button input type
		 */
		public InputType BUTTON = new InputType() {

			@Override
			public String toString() {
				return "button";
			}

			public void print(Appendable appendable) throws IOException {
				appendable.append("button");
			}

		};
	}

	private CharSequence name = null;

	private CharSequence value = null;

	private InputType type = null;

	private CharSequence onFocus = null;

	private CharSequence onBlur = null;

	private CharSequence onSelect = null;

	private Printable onChange = null;

	/**
	 * Creates an empty input
	 */
	public Input() {
		// left intentionally empty
	}

	/**
	 * Creates a input with the given type
	 * @param type The type of the input
	 */
	public Input(final InputType type) {
		Assert.notNull(type);
		this.type = type;
	}

	/**
	 * Creates an input with the given name and type
	 * @param name Name of the input control
	 * @param type The type of the input
	 */
	public Input(final CharSequence name, final InputType type) {
		this(type);
		Assert.hasText(name);
		this.name = name;
	}

	/**
	 * Creates an input with the given name, type and value.
	 * @param name Name of the input control
	 * @param type The type of the input
	 * @param value Value of the input control
	 */
	public Input(final CharSequence name, final InputType type, final CharSequence value) {
		this(name, type);
		this.value = value;
	}

	/**
	 * Creates an input with the given type and value.
	 * @param type The type of the input
	 * @param value Value of the input control
	 */
	public Input(final InputType type, final CharSequence value) {
		this(type);
		this.value = value;
	}

	@Override
	protected final void decorateCreatedTag(XMLTag tag) {
		if (type != null) {
			tag.addAttribute("type", type);
		}

		if (name != null) {
			tag.addAttribute("name", name);
		}

		if (value != null) {
			tag.addAttribute("value", value);
		}

		if (onFocus != null) {
			tag.addAttribute("onfocus", onFocus);
		}

		if (onBlur != null) {
			tag.addAttribute("onblur", onBlur);
		}

		if (onSelect != null) {
			tag.addAttribute("onselect", onSelect);
		}

		if (onChange != null) {
			tag.addAttribute("onchange", onChange);
		}
	}

	/**
	 * @return "input"
	 */
	@Override
	protected final CharSequence getTagName() {
		return "input";
	}

	public final InputType getType() {
		return type;
	}

	public final void setType(InputType type) {
		this.type = type;
	}

	public final CharSequence getName() {
		return name;
	}

	public final void setName(CharSequence name) {
		this.name = name;
	}

	public final CharSequence getValue() {
		return value;
	}

	public final void setValue(CharSequence value) {
		this.value = value;
	}

	public final CharSequence getOnBlur() {
		return onBlur;
	}

	public final void setOnBlur(CharSequence onBlur) {
		this.onBlur = onBlur;
	}

	public final Printable getOnChange() {
		return onChange;
	}

	public final void setOnChange(CharSequence onChange) {
		this.setOnChange(new PrintableCharSequence(onChange));
	}

	public final void setOnChange(Printable onChange) {
		this.onChange = onChange;
	}

	public final CharSequence getOnFocus() {
		return onFocus;
	}

	public final void setOnFocus(CharSequence onFocus) {
		this.onFocus = onFocus;
	}

	public final CharSequence getOnSelect() {
		return onSelect;
	}

	public final void setOnSelect(CharSequence onSelect) {
		this.onSelect = onSelect;
	}

}
